import { Injectable } from "@nestjs/common";
import { BehaviorSubject, Observable } from "rxjs";

interface IConnectState {
  [key: string]: boolean;
}

@Injectable()
export class ConnectStateService {
  private readonly connectionState = new BehaviorSubject<IConnectState>({
    system: false,
    dicom: false,
    gateway: false,
  });

  constructor() {}


  changeState(key, status) {
    const systemName = key.split("-")[1];
    if (systemName === undefined) {
      return
    }
    this.connectionState.next({
      ...this.connectionState.value,
      [systemName]: status,
    });
    console.log("changeState", key, status);
  }

  connectFE() {
    this.connectionState.next({
      ...this.connectionState.value,
      gateway: true,
    })
  }

  disconnectFE() {
    this.connectionState.next({
      ...this.connectionState.value,
      gateway: true,
    })
    console.log("Client disconnected:");
  }

  onConnectionChange(): Observable<IConnectState> {
    return this.connectionState.asObservable();
  }
}
